__author__ = 'Menju'



#get neo4j set up
#note, you have to have neo4j running and on the default port
import time
import sys
from py2neo import neo4j

graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data")

try:
    neo4j.CypherQuery(graph_db, """
        CREATE CONSTRAINT ON (u:User)
        ASSERT u.id_str IS UNIQUE
    """).execute()
except:
    pass


def convert():
    sep = ','
    #number to give each thing an individual name
    x = 0

    file = open("testfile.txt", "w")
    #open first file
    factionsFile = open("factions.txt", "r")
    factions = {}
    for line in factionsFile:
        thisLine = str(line)
        factions[thisLine.strip()] = "u" + str(x)
        query = "CREATE (u" + str(x) + ":Faction {name:'" + str(thisLine).strip() + "'})"
        file.write(query + "\n")
        x += 1
    factionsFile.close()

    x = 0

    planetFile = open("planets.txt", "r")
    x = 0
    planets = {}
    for line in planetFile:
        #print line
        hasSep = line.find(sep)
        data = []
        while (hasSep >= 0):
            hasSep = line.find(sep)
            tmp = line
            if (hasSep > 0):
                tmp = line[:line.find(sep)]
            #print tmp
            if (len(tmp) <= 0):
                break
            data.append(tmp.strip())
            line = line[line.find(sep) + 2:]

        file.write("CREATE (w" + str(x) + ":Planet {name:'" + data[0] + "',type:'" + data[1] + "',region:'" + data[
            2] + "',sector:'" + data[3] + "', system:'" + data[4] + "', coords:'" + data[5] + "'})\n")
        planets[str(data[0]).strip()] = "w"+str(x)
        x += 1
    planetFile.close()
    print(planets)

    x = 0

    personsFile = open("persons.txt", "r")
    persons = {}
    factionRelations = []
    planetRelations = []
    factionRelationsSize = 0
    planetRelationsSize = 0
    for line in personsFile:
        print(factions)
        #find seperator
        sepFound = 0
        hasSep = line.find(sep)
        data = []
        while (hasSep >= 0):
            sepFound = 1
            hasSep = line.find(sep)
            tmp = line
            if (hasSep > 0):
                tmp = line[:line.find(sep)]
            #print tmp
            if (len(tmp) <= 0):
                break
            data.append(tmp)
            print(tmp)
            line = line[line.find(sep) + 2:]
        if (sepFound > 0):
            if (sepFound > 0 and data[1] == 'Droide'):
                file.write(
                    "CREATE (v" + str(x) + ":Person {name:'" + data[0] + "',race:'" + data[1] + "',class:'" + data[
                        2] + "',type:'" + data[3] + "', born:'" + data[4] + "', died:'" + data[5] + "',creator:'" +
                    data[6] + "'})\n")
                myplanet = str(data[7]).strip()
                fac = str(data[8]).strip()
            else:
                file.write(
                    "CREATE (v" + str(x) + ":Person {name:'" + data[0] + "',race:'" + data[1] + "', born:'" + data[
                        2] + "', died:'" + data[3] + "'})\n")
                fac = str(data[5]).strip()
                myplanet = str(data[4]).strip()
            factionRelations.append("CREATE UNIQUE (v" + str(x) + ")-[:MEMBER]->(" + factions[fac] + ")")
            if(myplanet != '?') :
                planetRelations.append("CREATE UNIQUE (v" + str(x) + ")-[:FROM]->(" + planets[myplanet] + ")")
                planetRelationsSize += 1
            factionRelationsSize += 1
            persons[data[0]] = "v" + str(x)
        x += 1
    personsFile.close()

    for i in range(0, factionRelationsSize):
        file.write(factionRelations[i] + "\n")
        #file = open("newfile.txt", "w")

    for i in range(0, planetRelationsSize):
        file.write(planetRelations[i] + "\n")
        #file = open("newfile.txt", "w")

    print(persons)

    personsRelationsFile = open("relations_persons.txt", "r")
    for line in personsRelationsFile:
        sepFound = 0
        hasSep = line.find(sep)
        data = []
        while (hasSep >= 0):
            sepFound = 1
            hasSep = line.find(sep)
            tmp = line
            if (hasSep > 0):
                tmp = line[:line.find(sep)]
            #print tmp
            if (len(tmp) <= 0):
                break
            data.append(tmp.strip())
            print(tmp)
            line = line[line.find(sep) + 2:]
        if (sepFound > 0):
            file.write("CREATE UNIQUE (" + str(persons[data[0]]).strip() + ")-[:" + data[1] + "]->(" + str(
                persons[data[2]]).strip() + ")\n")
    personsRelationsFile.close()

    # planetRelationsFile = open("realtions_planets.txt", "r")
    # for line in planetRelationsFile:
    #     sepFound = 0
    #     hasSep = line.find(sep)
    #     data = []
    #     while (hasSep >= 0):
    #         sepFound = 1
    #         hasSep = line.find(sep)
    #         tmp = line
    #         if (hasSep > 0):
    #             tmp = line[:line.find(sep)]
    #         #print tmp
    #         if (len(tmp) <= 0):
    #             break
    #         data.append(tmp.strip())
    #         print(tmp)
    #         line = line[line.find(sep) + 2:]
    #     if (sepFound > 0):
    #         file.write("CREATE UNIQUE (" + str(persons[data[0]]).strip() + ")-[:" + data[1] + "]->(" + str(
    #             planets[data[2]]).strip() + ")\n")
    # planetRelationsFile.close()


convert()

